package com.chy.blog_web.modular.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_web.core.util.GetVideoOnePhotoUtil;
import com.chy.blog_web.core.util.UploadFileUtil;
import com.chy.blog_web.core.util.UploadURLUtil;
import com.chy.blog_web.modular.web.entity.*;
import com.chy.blog_web.modular.web.mapper.BoDynamicMapper;
import com.chy.blog_web.modular.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 描述：动态表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/04/04
 */
@Service
@Transactional(readOnly = true)
public class BoDynamicServiceImpl extends ServiceImpl<BoDynamicMapper, BoDynamic> implements IBoDynamicService {

    @Autowired
    private IBoDynamicImgService dynamicImgService;

    @Autowired
    private IBoLoveListService loveListService;

    @Autowired
    private IBoSeeBookService seeBookService;

    @Autowired
    private IBoSpaceUserLoboService spaceUserLoboService;

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }

    /**
     * 发动态、写感爱
     * @param dynamic
     * @param satisfied 满意度（0非常不满意，1不满意，2满意，3很满意，4非常满意）
     */
    @Transactional
    public void sendDynamic(BoDynamic dynamic, Integer satisfied, BoSpaceUser spaceUser){

        //保存动态
        dynamic.setSpaceUserId(spaceUser.getId());
        dynamic.setCreateDate(new Date());
        this.save(dynamic);

        //更新对应的恋爱、看书清单
        if(dynamic.getType() == 0){ //恋爱清单
            BoLoveList loveList = loveListService.getById(dynamic.getServiceId());
            if(spaceUser.getType() == 0){
                loveList.setHeSatisfied(satisfied);
                loveList.setHeSweet(satisfied + 1);
            }else if(spaceUser.getType() == 1){
                loveList.setHerSatisfied(satisfied);
                loveList.setHerSweet(satisfied + 1);
            }
            loveList.setStatus(1);
            loveListService.updateById(loveList);

            //生成写感受记录
            BoSpaceUserLobo spaceUserLobo = new BoSpaceUserLobo();
            spaceUserLobo.preInsert();
            spaceUserLobo.setServiceId(loveList.getId());
            spaceUserLobo.setSpaceUserId(spaceUser.getId());
            spaceUserLobo.setType(0);
            spaceUserLoboService.save(spaceUserLobo);

        }else if(dynamic.getType() == 1){
            BoSeeBook seeBook = seeBookService.getById(dynamic.getServiceId());
            seeBook.setStatus(2);
            seeBook.setSweet(10);
            seeBookService.updateById(seeBook);

            //生成写感受记录
            BoSpaceUserLobo spaceUserLobo = new BoSpaceUserLobo();
            spaceUserLobo.preInsert();
            spaceUserLobo.setServiceId(seeBook.getId());
            spaceUserLobo.setSpaceUserId(spaceUser.getId());
            spaceUserLobo.setType(1);
            spaceUserLoboService.save(spaceUserLobo);
        }
    }

    /**
     * 上传照片并生成缩略图
     * @param file
     * @param dynamicId
     * @return
     */
    @Transactional
    public Map<String, Object> uploadImg(MultipartFile file, String dynamicId){
        Map<String, Object> res = new HashMap<>();

        //文件类型处理
        String[] str = file.getOriginalFilename().split("\\.");
        String suffix = str[str.length - 1];

        BoDynamicImg dynamicImg = new BoDynamicImg();

        //上传
        Map<String, Object> map = new HashMap<>();
        if("mp4".equalsIgnoreCase(suffix)){ //mp4
            map = UploadFileUtil.fileUploadToOne(file, UploadURLUtil.DYNAMIC, false);
            dynamicImg.setFileType(1); //类型

            //获取视频的第一帧图片
            Random random = new Random();
            String fileName = "第一帧_" + System.currentTimeMillis() + random.nextInt(100000) + ".jpg";
            String filePath = UploadURLUtil.DYNAMIC + fileName; //保存的文件路径+文件名
            String videoName = UploadURLUtil.DYNAMIC + map.get("fileName"); //视频源
            GetVideoOnePhotoUtil.fetchFrame(videoName, filePath);
            dynamicImg.setOnePhoto(fileName);
        }else{
            map = UploadFileUtil.fileUploadToOne(file, UploadURLUtil.DYNAMIC, true);
            dynamicImg.setFileType(0); //类型
        }

        if("200".equals(map.get("state") + "")){
            dynamicImg.setImgUrl(map.get("fileName") + "");
            dynamicImg.setImgSize(Integer.valueOf(map.get("fileSize") + ""));
            dynamicImg.setThumUrl(map.get("thumFileName") + "");
            dynamicImg.setThumSize(Integer.valueOf(map.get("thumFileSize") + ""));
            dynamicImg.setDynamicId(dynamicId);
            dynamicImg.preInsert();
            dynamicImgService.save(dynamicImg);
            res.put("state", "200");
            res.put("message", "成功上传");
        }

        return res;
    }

}
